function [nrTargets, olTargets, olBins] = ST_OrderedTargetList2(targetList, seq_nr)
%ST_OrderedTargetList2 Summary of this function goes here
% Detailed explanation goes here
rows = size(targetList, 1);
cols = size(targetList, 2);

nrTargets = 0;
for row_nr = 1:rows
  for col_nr = 1:cols
    if targetList(row_nr, col_nr) <= 0
      break;
    else
      nrTargets = nrTargets + 1;
    end
  end
end

olTargets(1:nrTargets) = NaN;
olBins(1:nrTargets) = NaN;
current = 1;
for row_nr = 1:rows
  list_length = 0;
  for col_nr = 1:cols
    if targetList(row_nr, col_nr) <= 0
      break;
    else
      list_length = list_length + 1;
    end
  end
  
  nr_permutations = factorial(list_length);
  permutation = perms(targetList(row_nr, 1:list_length)); 
  current_perm = mod(seq_nr, nr_permutations);
  seq_nr = floor(seq_nr / nr_permutations);
  
  olTargets(current:(current + list_length - 1)) = permutation(current_perm + 1, :);
  olBins(current:(current + list_length - 1)) = row_nr;
  current = current + list_length;
end
end